package cj.web.marriage.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.EducationEnum;
import cj.web.marriage.constants.MemberDetailStatusEnum;
import cj.web.marriage.constants.SalaryEnum;
import cj.web.marriage.constants.XQIsChildrenEnum;
import cj.web.marriage.constants.XQIsMarryEnum;
import cj.web.marriage.constants.XQIsOpenEnum;
import cj.web.marriage.constants.MemberComTypeEnum;
import cj.web.marriage.constants.SexEnum;
import cj.web.marriage.constants.XQRoomOrCarEnum;
import cj.web.marriage.constants.XQStatusEnum;
import cj.web.marriage.dao.CompanyDAO;
import cj.web.marriage.dao.MemberDetailDAO;
import cj.web.marriage.domain.Company;
import cj.web.marriage.domain.MemberDetail;
import cj.web.marriageSC.dao.SCBuyerDAO;
import cj.web.marriageSC.domain.SCBuyer;

@Path("/memberDetail")
public class MemberDetailController extends BaseController{

	private static Logger logger = Logger.getLogger(MemberDetailController.class.getName());
	
	@Autowired
	private MemberDetailDAO memberDetailDao;
	@Autowired
	private CompanyDAO companyDao;
	@Autowired
	private SCBuyerDAO scBuyerDAO;
	
	@Get("show")
	public String show(Model model,MemberDetail memberDetail) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xqxqhygl);
        
        List<Company> companylis = companyDao.queryAll();
        model.add("companylis",companylis);
        
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("MemberDetailStatusEnum",EnumSet.allOf(MemberDetailStatusEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        
        return "/views/marriageXQ/memberDetailList.vm";
	}
	
	@Get("showsc")
	public String showsc(Model model,MemberDetail memberDetail) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.hyzlsc);
        
        List<Company> companylis = companyDao.queryAll();
        model.add("companylis",companylis);
        
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("MemberDetailStatusEnum",EnumSet.allOf(MemberDetailStatusEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        
        return "/views/marriageXQ/memberDetailScList.vm";
	}

	@Post("getList")
	public String queryList(MemberDetail memberDetail) {
		JSONObject jo = new JSONObject();
		
		List<MemberDetail> objs = memberDetailDao.query(memberDetail);
		for(MemberDetail obj:objs){
			if(obj.getType()==MemberComTypeEnum.NUM_0.getValue()){
				obj.setCompanyNameGL(obj.getCompanyName());
			}
		}
		int size = memberDetailDao.querySize(memberDetail);
		jo.put("data", objs);
		jo.put("draw", memberDetail.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xqxqhygl);
        List<Company> companylis = companyDao.queryAll();
        model.add("companylis",companylis);
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        
		return "/views/marriageXQ/memberDetailDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,MemberDetail memberDetail,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<SCBuyer> scBuyerlis = scBuyerDAO.queryByPhone(memberDetail.getAccount());
		
		if(scBuyerlis.size()>0){
			//存在
//			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
//			jo.put("returnCode", Constants.returnObj.returnCode.code_1);
//			return "@"+jo.toString();
			
			List<SCBuyer> userlis = scBuyerDAO.queryByPhone(memberDetail.getAccount());
			memberDetail.setUserId(userlis.get(0).getId());
			
		}else{
			//不存在
			SCBuyer obj = new SCBuyer();
			obj.setPhone(memberDetail.getAccount());
			obj.setName(memberDetail.getName());
			obj.setCreateUser(returnSessionObj(request).getAccount());
			obj.setUpdateUser(returnSessionObj(request).getAccount());
			int userId = scBuyerDAO.create(obj);
			memberDetail.setUserId(userId);
		}
		
		try {
			
			if(MemberComTypeEnum.NUM_1.getValue()==memberDetail.getType()){
				//1单位
				//公司信息
				Company company = companyDao.queryById(memberDetail.getCompanyId());
				
				memberDetail.setCompanyName(company.getName()==null?"":company.getName());
			}else{
				memberDetail.setCompanyId(-1);
			}
			
			memberDetail.setStatus(XQStatusEnum.NUM_0.getValue());
			memberDetail.setCreateUser(returnSessionObj(request).getAccount());
			memberDetail.setUpdateUser(returnSessionObj(request).getAccount());
			memberDetailDao.create(memberDetail);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		MemberDetail obj = memberDetailDao.queryById(id);
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xqxqhygl);
        List<Company> companylis = companyDao.queryAll();
        model.add("companylis",companylis);
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        
		return "/views/marriageXQ/memberDetailEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,MemberDetail memberDetail,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			if(MemberComTypeEnum.NUM_1.getValue()==memberDetail.getType()){
				//公司信息
				Company company = companyDao.queryById(memberDetail.getCompanyId());
				
				memberDetail.setCompanyName(company.getName()==null?"":company.getName());
			}else{
				memberDetail.setCompanyId(-1);
			}
			
			memberDetail.setUpdateUser(returnSessionObj(request).getAccount());
			memberDetailDao.update(memberDetail);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("detail")
	public String detail(Model model,@Param("userId") int userId)
	{
		List<MemberDetail> obj = memberDetailDao.queryByUserId(userId);
		if(obj!=null && obj.size()>0){
			if(obj.get(0).getType()==MemberComTypeEnum.NUM_0.getValue()){
				obj.get(0).setCompanyNameGL(obj.get(0).getCompanyName());
			}
			model.add("memberDetailObj",obj.get(0));
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xqxqhygl);
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        
		return "/views/marriageXQ/memberDetailInfor.vm";
	}
	
	@Get("infor")
	public String infor(Model model,@Param("id") int id)
	{
		MemberDetail obj = memberDetailDao.queryById(id);
		if(obj!=null){
			if(obj.getType()==MemberComTypeEnum.NUM_0.getValue()){
				obj.setCompanyNameGL(obj.getCompanyName());
			}
			model.add("memberDetailObj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xqxqhygl);
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        
		return "/views/marriageXQ/memberDetailInfor.vm";
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		memberDetailDao.deleteById(id);
		return "r:showsc";
	}
	
	@Get("ok")
	public String ok(Model model,@Param("id") int id)
	{
		memberDetailDao.updateStatusById(id,MemberDetailStatusEnum.NUM_1.getValue());
		return "r:show";
	}
	
	@Get("remove")
	public String remove(Model model,@Param("id") int id)
	{
		memberDetailDao.updateStatusById(id,MemberDetailStatusEnum.NUM_0.getValue());
		return "r:show";
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (file != null && !file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath_Marriage");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 120, 120, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
}